#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
from . import comparecards as cmp_cards


def fromWaste(cls, pile, pilegroup):
    assert len(cls.s['wastes']) > 0
    if not pile.cards and pile in pilegroup:
        if cls.s['wastes'][-1].cards:
            cls.s['wastes'][-1].move_pile(1, pile)
        else:
            cls.talon_deal()


def fromTalon(cls, pile, pilegroup):
    if not pile.cards and pile in pilegroup:
        talon = cls.s['talon']
        for i in range(min(len(talon.cards), talon.num_deal)):
            if not talon.TOPCARD.face_up:
                cls.flip(talon)
            cls.move(1, talon, pile)


def fromTalonFinish(cls, pile, pilegroup):
    if not pile.cards and pile in pilegroup:
        talon, foundations = cls.s['talon'], cls.s['foundations']
        for i in range(min(len(talon.cards), talon.num_deal)):
            while pile.cards < i and talon.cards:
                stack = cmp_cards.groupTakes(foundations, talon) or pile
                if not talon.TOPCARD.face_up:
                    cls.flip(talon)
                cls.move(1, talon, stack)


def TalonFinishAce(cls, pile):
    if pile is cls.s['talon'] and pile.cards:
        talon, foundations = pile, cls.s['foundations']
        while talon.TOPCARD['rank'] == 0:
            stack = cmp_cards.groupTakes(foundations, talon)
            cls.move(1, talon, stack)
            if stack.cardFlips():
                cls.flip(stack)
            if not stack.cards:
                break


def fromReserve(cls, pile, pilegroup):
    if len(cls.s.reserves) == 1 and not pile.cards and pile in pilegroup:
        reserve = cls.s['reserves'][0]
        for i in range(min(len(reserve.cards), 1)):
            if not reserve.TOPCARD.face_up:
                cls.flip(reserve)
            cls.move(1, reserve, pile)


def fromReserveOrWaste(cls, pile, pilegroup):
        if not pile.cards and pile in pilegroup:
            r1, r2 = cls.s['reserves'][:2]
            from_stack = None
            if r1.cards or r2.cards:
                from_stack = r1
                if len(r1.cards) < len(r2.cards):
                    from_stack = r2
            elif cls.s['wastes'] and cls.s['wastes'][-1].cards:
                from_stack = cls.s['wastes'][-1]
            if from_stack:
                cls.move(1, from_stack, pile)